package cn.stylefeng.guns.modular.controller;

import cn.stylefeng.guns.base.auth.annotion.Permission;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.modular.dto.ChangeScheduleFormItemDto;
import cn.stylefeng.guns.modular.dto.ScheduleFormItemDto;
import cn.stylefeng.guns.modular.dto.ScheduleFrameDto;
import cn.stylefeng.guns.modular.dto.ScheduleFrameItemDto;
import cn.stylefeng.guns.modular.entity.ScheduleFormItem;
import cn.stylefeng.guns.modular.entity.ScheduleFrame;
import cn.stylefeng.guns.modular.entity.ScheduleFrameItem;
import cn.stylefeng.guns.modular.service.ScheduleFormItemService;
import cn.stylefeng.guns.modular.service.ScheduleFrameItemService;
import cn.stylefeng.guns.modular.service.ScheduleFrameService;
import cn.stylefeng.guns.sys.core.constant.Const;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.net.SocketTimeoutException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

@Controller
@RequestMapping("/myScheduleFormItem")
public class MyScheduleFormItemController extends BaseController {

    @Autowired
    private ScheduleFormItemService scheduleFormItemService;

    @Autowired
    private ScheduleFrameItemService scheduleFrameItemService;

    /**
     * 创建日程项页面
     * @return
     */
    @RequestMapping("/createSformItem")
    public String createSformItem(ScheduleFormItemDto scheduleFormItemDto, Model model){
        System.out.println("=========123lsw===="+scheduleFormItemDto);
        System.out.println("============"+scheduleFormItemDto.getSformId());
        String sframeId=scheduleFormItemDto.getSframeId().toString();
        model.addAttribute("sframeId",sframeId);
        model.addAttribute("scheduleFormItemDto",scheduleFormItemDto);
        //返回创建日程项弹窗页面
        return "/scheduleformItem/formItem.html";
    }

    /**
     * 保存日程项
     * @return
     */
    @RequestMapping("saveSfromItem")
    @ResponseBody
    public ResponseData saveSfromItem(ScheduleFormItemDto scheduleFormItemDto){

        System.out.println("======lswlsw======"+scheduleFormItemDto);

        String sframeItemId=scheduleFormItemDto.getTimeLimit();
       ScheduleFrameItem scheduleFrameItem= scheduleFrameItemService.selectSframeItemById(Integer.parseInt(sframeItemId));
        System.out.println("============123123lsw========="+scheduleFrameItem);

        ScheduleFormItem scheduleFormItem =new ScheduleFormItem();

        //格式化时间
        Date sformItemDate=null;
        Date startTime=null;
        Date stopTime=null;
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormatHours=new SimpleDateFormat("hh:mm:ss");
        SimpleDateFormat simpleDateFormatAll=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println("=========123422========="+simpleDateFormatHours.format(scheduleFrameItem.getStartTime()));
        String startTimeHours= simpleDateFormatHours.format(scheduleFrameItem.getStartTime());
        String stopTimeHours= simpleDateFormatHours.format(scheduleFrameItem.getStopTime());

        String startTimeStr=scheduleFormItemDto.getSformItemDate()+" "+startTimeHours;
        System.out.println("===========startTimeStr123=========="+startTimeStr);
        String stopTimeStr=scheduleFormItemDto.getSformItemDate()+" "+stopTimeHours;
        System.out.println("===============stopTimeStr123123================="+stopTimeStr);

        try {
             sformItemDate= simpleDateFormat.parse(scheduleFormItemDto.getSformItemDate());
            System.out.println("============123313=========="+sformItemDate);


            startTime=simpleDateFormatAll.parse(startTimeStr);
            stopTime=simpleDateFormatAll.parse(stopTimeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        scheduleFormItem.setSformItemDate(sformItemDate);

        scheduleFormItem.setSformItemName(scheduleFormItemDto.getSformItemName());
        scheduleFormItem.setLocation(scheduleFormItemDto.getLocation());

        scheduleFormItem.setStartTime(startTime);
        scheduleFormItem.setStopTime(stopTime);
        scheduleFormItem.setSframeItemId(Integer.parseInt(scheduleFrameItem.getSerialNumber()));
        scheduleFormItem.setSformId(scheduleFormItemDto.getSformId());
        scheduleFormItem.setComment(scheduleFormItemDto.getComment());

        int rows =scheduleFormItemService.saveScheduleFormItem(scheduleFormItem);
        if (rows==1){
            System.out.println("添加成功！");
            return  SUCCESS_TIP;
        }else {
            return  ResponseData.error("日程项保存失败！");
        }

    }


    /**
     * 保存修改日程项
     * @return
     */
    @RequestMapping("saveChangeSfromItem")
    @ResponseBody
    public ResponseData saveChangeSfromItem(ChangeScheduleFormItemDto changeScheduleFormItemDto){
        System.out.println("======lswlsw======"+changeScheduleFormItemDto);
        //框架项Id
       String sfrmaeItemId= changeScheduleFormItemDto.getTimeLimit();
       //得到框架项信息
       ScheduleFrameItem scheduleFrameItem= scheduleFrameItemService.selectSframeItemById(Integer.parseInt(sfrmaeItemId));
        SimpleDateFormat simpleDateFormatYear=new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormatTime=new SimpleDateFormat("hh:mm:ss");
        SimpleDateFormat simpleDateFormatAll=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        Date startTimeDate= scheduleFrameItem.getStartTime();
        Date stopTimeDate=scheduleFrameItem.getStopTime();
        System.out.println(stopTimeDate);
        //获得时分秒
        String  startTimeStr=simpleDateFormatTime.format(startTimeDate);
        String  stopTimeStr=simpleDateFormatTime.format(stopTimeDate);

        //存放修改后的信息
        ScheduleFormItem scheduleFormItem =new ScheduleFormItem();
        scheduleFormItem.setSformItemName(changeScheduleFormItemDto.getSformItemName());
        scheduleFormItem.setLocation(changeScheduleFormItemDto.getLocation());

        String sfromItemDateStr = changeScheduleFormItemDto.getSformItemDate();
        //得到开始的全时间
        String startTimeAll=sfromItemDateStr+" "+startTimeStr;
        //得到结束的全时间
        String stopTimeAll=sfromItemDateStr+" "+stopTimeStr;


        Date startTime =null;
        Date stopTime=null;
        Date sformItemDate=null;
        try {
             startTime =simpleDateFormatAll.parse(startTimeAll);
            stopTime =simpleDateFormatAll.parse(stopTimeAll);
            sformItemDate=simpleDateFormatYear.parse(changeScheduleFormItemDto.getSformItemDate());
            System.out.println("=======now========="+startTime);
            System.out.println("=======now========="+stopTime);
            System.out.println("=======now========="+sformItemDate);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        scheduleFormItem.setSformItemDate(sformItemDate);
        scheduleFormItem.setStartTime(startTime);
        scheduleFormItem.setStopTime(stopTime);
        scheduleFormItem.setSframeItemId(Integer.parseInt(scheduleFrameItem.getSerialNumber()));
        scheduleFormItem.setSformId(changeScheduleFormItemDto.getSformId());
        scheduleFormItem.setSformItemId(Integer.parseInt(changeScheduleFormItemDto.getSformItemId().trim()));

        int rows =scheduleFormItemService.updateSformItem(scheduleFormItem);
        if (rows==1){
            System.out.println("修改成功！");
            return  SUCCESS_TIP;
        }else {
            return  ResponseData.error("日程项修改失败！");
        }
    }

    /**
     * 显示该日程表某框架项和某天的所有日程项
     *
     */
    @RequestMapping("/showCurrSformItems")
    @ResponseBody
    public List<ScheduleFormItem> showCurrSformItems( String sformId,  String sframeItemSerail,  String sformItemDate){

        List<ScheduleFormItem> scheduleFormItems=scheduleFormItemService.showCurrSformItem(Integer.parseInt(sformId),Integer.parseInt(sframeItemSerail),sformItemDate);
        System.out.println("======================="+scheduleFormItems);
        return scheduleFormItems;
    }


    /**
     * 加载修改日程项页面
     * @param sformItemId
     * @param model
     * @return
     */
    @RequestMapping("/loadUpdateSformItem")
    public String loadUpdateSformItem(String sformItemId,String sformId, String sframeId,Model model){

        System.out.println("==============="+sformItemId+"======="+sformId+"==================="+sframeId);
        System.out.println("=======================是多少故事=");
        ScheduleFormItem scheduleFormItem=scheduleFormItemService.selectSformItemById(Integer.parseInt(sformItemId.trim()));
        System.out.println("==============="+sformItemId+"======="+sformId+"==================="+sframeId);


        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String sformItemDate=simpleDateFormat.format(scheduleFormItem.getSformItemDate()).split(" ")[0];
        System.out.println("=======now123====="+sformItemDate);



        model.addAttribute("scheduleFormItem",scheduleFormItem);
        model.addAttribute("sframeId",sframeId);
        model.addAttribute("sformItemId",sformItemId);
        model.addAttribute("sformItemDate",sformItemDate);

        return "/scheduleformItem/updateSformItem.html";
    }


    /**
     *根据sformItemId进行删除
     * @param sformItemId
     * @return
     */
    @RequestMapping(value = "/deleteSformItemById",method = RequestMethod.POST)
    @ResponseBody
    public String deleteSformItemById(String sformItemId){
       int rows= scheduleFormItemService.deleteSformItemById(Integer.parseInt(sformItemId));
       if (rows==1){
           return "日程项删除成功！";
       }else {
           return "日程项删除失败！";
       }

    }



}




